package com.mini.cn.goods.controller;

import com.alibaba.fastjson.JSONObject;
import com.mini.cn.goods.po.OrderModel;
import com.mini.cn.goods.po.dto.OrderDto;
import com.mini.cn.goods.service.OrderService;
import com.mini.cn.goods.util.ErrorMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA.
 * User: fly
 * Date: 2022/11/20
 * Time: 18:24
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@Slf4j
public class GroupOrderController {
    @Autowired
    OrderService orderService;

    @RequestMapping(value = "/groupOrder", method = RequestMethod.POST, produces = "application/json;charset=utf-8", consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap groupOrder(@RequestBody OrderDto orderDto) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            //发送支付成功的消息
            OrderModel orderModel = orderService.groupOrder(orderDto);
            if (orderModel != null) {
                int isfree = orderModel.isfree;
                String orderid = orderModel.orderid;
                supplyMap.put("isfree", isfree);
                supplyMap.put("order_id", orderid);
                supplyMap.put("addsore", orderModel.addsore);
                if (isfree == 0) {
                    //如果不是免费
                    String appId = orderModel.appId;
                    log.info("appid=" + appId);
                    supplyMap.put("appId", appId);
                    String nonceStr = orderModel.nonceStr;
                    supplyMap.put("nonceStr", nonceStr);
                    log.info("nonceStr=" + nonceStr);
                    String paySign = orderModel.paySign;
                    supplyMap.put("paySign", paySign);
                    log.info("paySign=" + paySign);
                    String signType = orderModel.signType;
                    supplyMap.put("signType", signType);
                    String timeStamp = orderModel.timeStamp;
                    supplyMap.put("timeStamp", timeStamp);
                    supplyMap.put("package", orderModel.preid);
                    log.info("package=" + orderModel.preid);
                }
            } else {
                supplyMap.setEorrInfo(ErrorMsg.STATUS_ORDER_FAILURE, ErrorMsg.MSG_ORDER_FAILURE);
            }
        } catch (Exception e) {
            log.error("group order:",e);
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        log.info("order josn=" + JSONObject.toJSONString(supplyMap));
        return supplyMap;
    }
}
